package com.tsll.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsll.dao.EbookMapper;
import com.tsll.entity.Ebook;
import com.tsll.service.EbookService;
import com.tsll.vo.EbookVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EbookServiceImpl implements EbookService {
    @Autowired
    private EbookMapper ebookMapper;
//    获取电子书籍流行书单数据成功
    public Result getEbook() {
      return new Result(200,"获取电子书籍流行书单数据成功",ebookMapper.getPopular());
    }

    @Override
//    获取电子书单
    public Result findByPage(Integer current, Integer pageSize, EbookVo ebookVo) {
        //启动分页
        PageHelper.startPage(current,pageSize);
        List<Ebook> ebooks = ebookMapper.selectByVo(ebookVo);
        //封装到PgeInfo
        PageInfo<Ebook> ebookPageInfo = new PageInfo<Ebook>(ebooks);
        return new Result(200,"查询书籍成功",ebookPageInfo);
    }
    @Override
//    添加电子图书
    public Result insert(Ebook ebook) {
        int ad = ebookMapper.add(ebook);
        if(ad==1){
            return new Result(200,"添加成功",ad);
        }else {
            return new Result(500,"添加失败",null);
        }
    }
    @Override
    //修改电子图书
    public Result update(Ebook ebook) {
        int edit = ebookMapper.edit(ebook);
        if(edit==1){
            return new Result(200,"修改成功",ebook);
        }else {
            return new Result(500,"修改失败",null);
        }
    }
    @Override
    //删除电子图书
    public Result delBook(Integer ebookId) {
        int del = ebookMapper.deleteById(ebookId);
        if(del==1){
            return new Result(200,"删除成功",null);
        }else {
            return new Result(500,"删除失败",null);
        }
    }
    @Override
    //获取最近半年内流行的电子图书信息
    public Result AllNewDate() {
        List<Ebook> ebooks = ebookMapper.selectNewDate();
        return new Result(200,"获取最新日期（最近半年内发布的）",ebooks);
    }
}
